package ro.ase.catalog.security.dao.hibernate;

import ro.ase.catalog.beans.Grade;
import ro.ase.catalog.beans.Student;
import ro.ase.catalog.beans.Teacher;
import ro.ase.catalog.beans.dto.GradeDTO;
import ro.ase.catalog.dao.hibernate.GenericDAOImpl;
import ro.ase.catalog.security.dao.IGradeDAO;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Ioana
 * Date: 6/14/14
 * Time: 2:48 PM
 * To change this template use File | Settings | File Templates.
 */
public class GradeDAOImpl extends GenericDAOImpl<Grade, Integer> implements IGradeDAO {

    public List<GradeDTO> displayGradesByStudent(Student student, Teacher teacher) {
        List<GradeDTO> gradeDTOs=new ArrayList<GradeDTO>();
        List<Grade> gradeList=getHibernateTemplate().find("select g from Grade g where g.student = ? and g.teacherDisciplineClass.teacher=?", new Object[]{student, teacher} );
        for(int i=0;i<gradeList.size();i++)
        {
          GradeDTO gradeDTO=new GradeDTO();
            gradeDTO.setId(gradeList.get(i).getId());
            gradeDTO.setValue(gradeList.get(i).getValue());
            gradeDTO.setComment(gradeList.get(i).getComment());
            gradeDTO.setDate(gradeList.get(i).getDate().toString().substring(0, 10));
            if((gradeList.get(i).getTeza()!=null)&&((gradeList.get(i).getTeza()).equals(true)))
                gradeDTO.setGradeState("Da");
            else
                gradeDTO.setGradeState("");
            gradeDTOs.add(gradeDTO);
        }
            return gradeDTOs;

    }

    public List<GradeDTO> displayGradesByStudent(Student student, Teacher teacher, Date date) {
        List<GradeDTO> gradeDTOs=new ArrayList<GradeDTO>();
        List<Grade> gradeList=getHibernateTemplate().find("select g from Grade g where g.student = ? and g.teacherDisciplineClass.teacher=? and g.date<=?", new Object[]{student, teacher,date} );
        for(int i=0;i<gradeList.size();i++)
        {
            GradeDTO gradeDTO=new GradeDTO();
            gradeDTO.setId(gradeList.get(i).getId());
            gradeDTO.setValue(gradeList.get(i).getValue());
            gradeDTO.setComment(gradeList.get(i).getComment());
            gradeDTO.setDate(gradeList.get(i).getDate().toString().substring(0, 10));
            if((gradeList.get(i).getTeza()!=null)&&((gradeList.get(i).getTeza()).equals(true)))
                gradeDTO.setGradeState("Da");
            else
                gradeDTO.setGradeState("");
            gradeDTOs.add(gradeDTO);
        }
        return gradeDTOs;

    }
}
